package Game;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.AdvancedPlayer;

import java.awt.*;
import java.io.InputStream;

/**
 * 音乐类
 */
public class GameMusic {
 //创建两个线程来控制音乐
 //1.背景音乐 2.操作音乐
 public class  SoundThread implements Runnable{
  public String MusicUrl;
  public SoundThread(String url){
   this.MusicUrl=url;
  }

     @Override
     public void run() {
      //播放音乐
         //1.加载音乐
         InputStream in= GameMusic.class.getClassLoader().getResourceAsStream(MusicUrl);

         //根据文件流,创建播放类对象
         AdvancedPlayer ad= null ;
         try {
             ad = new AdvancedPlayer(in);
             ad.play();/*播放*/
         } catch (JavaLayerException e) {
             throw new RuntimeException(e);
         }

     }


 }

 public class BgSoundThred implements Runnable {
  public String MusicUrl;
  public BgSoundThred(String url){
   this.MusicUrl=url;
  }

     @Override
     public void run() {
      //播放音乐
         //1.加载音乐
         InputStream in= GameMusic.class.getClassLoader().getResourceAsStream(MusicUrl);

         //根据文件流,创建播放类对象
         try {
             AdvancedPlayer ad= new AdvancedPlayer(in);
             ad.play();
         } catch (JavaLayerException e) {
             throw new RuntimeException(e);
         }
     }
 }

 public class WinSoundThred implements  Runnable{
     public String Music;
     public  WinSoundThred(String url){
         this.Music=url;
     }

     @Override
     public void run() {
         InputStream in=GameMusic.class.getClassLoader().getResourceAsStream(Music);
         try {
             AdvancedPlayer ad=new AdvancedPlayer(in);
             ad.play();
         } catch (JavaLayerException e) {
             throw new RuntimeException(e);
         }
     }
 }

   Thread bst=null;
    /**
     * 调用播放背景音乐
     * @param url
     */
 public void playBgSound(String url){
     if(bst==null) {
         Thread T1=new Thread(new BgSoundThred(url));
         bst=T1;
         T1.start();
     }
 }

  public void playBgSound(int type){
    if(type==1 && bst!=null){
         bst.stop();
         bst=null;
    }
 }


    /**
     * 播放一次
     * @param url
     */
 public Thread playSound(String url){
     //如果存在就停止
     Thread T2=new Thread(new SoundThread(url));
     T2.start();

     return  T2;
 }

 public void  playWinSound(String url){
     Thread T3=new Thread(new WinSoundThred(url));
     T3.start();
 }
}
